<template>
  <div id="toolbar" class="toolbar">
    <div class="canvasToolbar___3oTpH">
      <div class="x6-toolbar x6-toolbar-hover-effect">
        <div class="x6-toolbar-content">
          <div class="x6-toolbar-content-inner">
            <!-- <div class="x6-toolbar-group"> -->
            <!-- <tooltip content="放大"> -->
            <button class="x6-toolbar-item" @click="zoomIn">
              <span class="x6-toolbar-item-icon"><span role="img" aria-label="group" class="anticon anticon-group">
                  <svg-icon icon-class="zoom-in"></svg-icon>
                  <span class="svg-text">放大</span>
                </span></span>
            </button>
            <!-- </tooltip>
              <tooltip content="缩小"> -->
            <button class="x6-toolbar-item" @click="zoomOut">
              <span class="x6-toolbar-item-icon"><span role="img" aria-label="ungroup" class="anticon anticon-ungroup">
                  <svg-icon icon-class="zoom-out"></svg-icon>
                  <span class="svg-text">缩小</span>
                </span></span>
            </button>
            <!-- </tooltip> -->
            <!-- </div>
            <div class="x6-toolbar-group"> -->
            <!-- <tooltip content="撤销"> -->
            <button :disabled="btnDisabled" class="x6-toolbar-item" @click="undo"
              :class="{ 'x6-toolbar-item-disabled': readOnly||btnDisabled }">
              <span class="x6-toolbar-item-icon">
                <span role="img" aria-label="rollback" class="anticon anticon-rollback">
                  <svg-icon icon-class="undo"></svg-icon>
                  <span class="svg-text">撤销</span>
                </span></span>
            </button>
            <!-- </tooltip> -->
            <!-- <tooltip content="重做"> -->
            <!-- <button class="x6-toolbar-item" @click="redo" :disabled="btnDisabled" :class="{ 'x6-toolbar-item-disabled': readOnly||btnDisabled }">
              <span class="x6-toolbar-item-icon"><span role="img" aria-label="gateway" class="anticon anticon-gateway">
                  <svg-icon icon-class="redo"></svg-icon>
                  <span class="svg-text">重做</span>
                </span></span>
            </button> -->
            <!-- </tooltip> -->
            <!-- </div>

            <div class="x6-toolbar-group"> -->
            <!-- <tooltip content="删除"> -->
            <button class="x6-toolbar-item" @click="deleteNode" :disabled="btnDisabled" :class="{ 'x6-toolbar-item-disabled': readOnly||btnDisabled }">
              <span class="x6-toolbar-item-icon"><span role="img" aria-label="play-square"
                  class="anticon anticon-play-square">
                  <svg-icon icon-class="delete"></svg-icon>
                  <span class="svg-text">删除</span>
                </span></span>
            </button>
            <!-- </tooltip>
              <tooltip content="清除"> -->
            <button class="x6-toolbar-item" @click="clear" :disabled="btnDisabled" :class="{ 'x6-toolbar-item-disabled': readOnly||btnDisabled }">
              <span class="x6-toolbar-item-icon"><span role="img" aria-label="gateway" class="anticon anticon-gateway">
                  <svg-icon icon-class="clear"></svg-icon>
                  <span class="svg-text">清除</span>
                </span></span>
            </button>
            <!-- </tooltip> -->
            <!-- </div>
            <div class="x6-toolbar-group"> -->
            <!-- <tooltip content="刷新"> -->
            <button class="x6-toolbar-item" @click="flush" :disabled="btnDisabled"  :class="{ 'x6-toolbar-item-disabled': readOnly||btnDisabled }">
              <span class="x6-toolbar-item-icon"><span role="img" aria-label="play-square"
                  class="anticon anticon-play-square">
                  <svg-icon icon-class="flush"></svg-icon>
                  <span class="svg-text">刷新</span>
                </span></span>
            </button>
            <!-- </tooltip> -->
            <!-- </div> -->
            <!-- <div class="x6-toolbar-group">
              <input
                class="rt-input"
                v-model="keyword"
                @input="search"
                @keypress.enter="next"
                placeholder="搜索组件，回车键下一个"
              /> -->
            <slot />
            <!-- <div class="x6-toolbar-group">
              
            </div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
w
<script>
import { MessageBox } from "element-ui";
export default {
  inject: ["app"],
  components: {},
  data() {
    return {
      canUndo: false,
      canRedo: false,
      canSave: false,
      keyword: "",
      searchNodes: [],
      currentIndex: 0,
    };
  },
  computed: {
    readOnly() {
      return this.app.readOnly;
    },
    btnDisabled() {
      return this.app.btnDisabled
    }
  },
  methods: {
    zoomIn() {
      this.app.graph.zoom(0.1);
    },
    zoomOut() {
      this.app.graph.zoom(-0.1);
    },
    undo() {
      if (this.readOnly) return;
      this.app.graph.undo();
      this.updateDo();
      this.$emit("undo");
    },
    redo() {
      if (this.readOnly) return;
      this.app.graph.redo();
      this.updateDo();
      this.$emit("redo");
    },
    updateDo() {
      if (this.readOnly) return;
      this.canUndo = this.app.graph.canUndo();
      this.canRedo = this.app.graph.canRedo();
    },
    deleteNode() {
      if (this.readOnly) return;
      if (this.app.graph.getSelectedCells().length === 0) return;
      MessageBox.confirm("确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const nodes = this.app.graph.getSelectedCells();
          this.app.graph.removeCells(nodes);
          this.$emit("deleteNode", nodes);
        })
        .catch(() => { });
    },
    flush() {
      if (this.readOnly) return;
      this.$emit("flush");
    },
    download() {
      const data = this.app.graph.toJSON();
      console.log(JSON.stringify(data));
      this.$emit("download", data);
    },
    search() {
      const nodes = this.app.graph.getNodes();
      this.searchNodes = [];
      this.currentIndex = 0;
      nodes.forEach((node) => {
        if (node.data.text.includes(this.keyword)) {
          this.searchNodes.push(node);
        }
      });
      if (this.searchNodes.length > 0) {
        this.app.graph.centerCell(this.searchNodes[0]);
      }
    },
    next() {
      if (this.searchNodes.length === 0) return;
      this.currentIndex++;
      if (this.searchNodes.length <= this.currentIndex) {
        this.currentIndex = 0;
      }
      this.app.graph.centerCell(this.searchNodes[this.currentIndex]);
    },
    clear() {
      if (this.readOnly) return;
      MessageBox.confirm("确认清除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.app.graph.fromJSON({});
          this.$emit("clear");
        })
        .catch(() => { });
    },
  },
};
</script>

<style scoped lang="less">
.toolbar {
  height: 36px;
  overflow: visible;
  border-bottom: 1px solid #ececec;
  width: 100%;
}

.x6-toolbar {
  display: flex;
  flex-direction: row;
  height: 28px;
  margin: 0;
  padding: 0;
  padding-left: 4px;
  overflow: hidden;
}

.x6-toolbar-content {
  display: flex;
  flex: 1 1;
  flex-direction: row;
  justify-content: space-between;
  overflow: hidden;
}

.x6-toolbar-content-inner,
.x6-toolbar-content-extras,
.x6-toolbar-group,
.x6-toolbar-item,
.x6-toolbar-item-icon,
.x6-toolbar-item-text {
  display: flex;
  flex-direction: row;
}

.x6-toolbar-content-extras {
  align-content: center;
  align-items: center;
  font-size: 14px;
}

.x6-toolbar-group::before {
  align-self: center;
  width: 1px;
  height: 40%;
  margin: 0 6px;
  background-color: rgba(0, 0, 0, 0.15);
  content: " ";
  pointer-events: none;
}

.x6-toolbar-group:first-child::before {
  content: none;
}

.x6-toolbar-item {
  align-content: center;
  align-items: center;
  margin: 0;
  padding: 0 4px;
  color: #595959;
  background-color: transparent;
  border: 0;
  border-radius: 2px;
  outline: none;
  box-shadow: none;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.x6-toolbar-item.x6-toolbar-item-hidden {
  display: none;
}

.x6-toolbar-item.x6-toolbar-item-disabled,
.x6-toolbar-item.x6-toolbar-item-disabled:hover {
  cursor: not-allowed;
  opacity: 0.4;
}

.x6-toolbar-item.x6-toolbar-item-disabled .x6-toolbar-item-icon .x6-toolbar-item:active {
  transform: none;
}

.x6-toolbar-item-icon,
.x6-toolbar-item-text {
  align-content: center;
  align-items: center;
}

.x6-toolbar-item-icon {
  font-size: 14px;
  transition: transform 50ms ease;
}

.x6-toolbar-item-icon:active {
  transform: scale(1.27201965);
}

.x6-toolbar-item-text {
  padding-left: 4px;
  font-size: 12px;
}

.x6-toolbar-item-dropdown-wrap {
  margin-top: -13px;
}

.x6-toolbar-item-dropdown-arrow {
  width: 0;
  height: 0;
  margin-left: 6px;
  border-color: #262626 transparent transparent transparent;
  border-style: solid;
  border-width: 5px 4px 0 4px;
  opacity: 0.4;
}

.x6-toolbar.x6-toolbar-hover-effect {
  height: 32px;
}

 

.x6-toolbar.x6-toolbar-hover-effect .x6-toolbar-item-disabled.x6-toolbar-item:hover {
  color: #595959;
  background-color: transparent;
}

.x6-toolbar.x6-toolbar-hover-effect .x6-toolbar-item-disabled.x6-toolbar-item:hover .x6-toolbar-item-dropdown-arrow {
  opacity: 0.4;
}

.x6-toolbar-item.x6-toolbar-item-active,
.x6-toolbar.x6-toolbar-hover-effect .x6-toolbar-item.x6-toolbar-item-active,
.x6-toolbar.x6-toolbar-hover-effect .x6-toolbar-item:hover {
  color: #262626;
  background-color: #e0e0e0;
}

.x6-toolbar-item.x6-toolbar-item-active .x6-toolbar-item-dropdown-arrow,
.x6-toolbar.x6-toolbar-hover-effect .x6-toolbar-item.x6-toolbar-item-active .x6-toolbar-item-dropdown-arrow,
.x6-toolbar.x6-toolbar-hover-effect .x6-toolbar-item:hover .x6-toolbar-item-dropdown-arrow {
  opacity: 0.75;
}

.x6-toolbar-item.x6-toolbar-item-active {
  margin: 4px 0;
}

.x6-toolbar.x6-toolbar-big {
  height: 32px;
}

.x6-toolbar.x6-toolbar-big .x6-toolbar-content-extras {
  font-size: 16px;
}

 

.x6-toolbar.x6-toolbar-big .x6-toolbar-item-text {
  font-size: 14px;
}

.x6-toolbar.x6-toolbar-big .x6-toolbar-item-icon {
  font-size: 16px;
}

.x6-toolbar.x6-toolbar-hover-effect.x6-toolbar-big {
  height: 40px;
}

.x6-toolbar.x6-toolbar-hover-effect.x6-toolbar-big .x6-toolbar-item {
  padding: 0 10px;
}

.x6-toolbar.x6-toolbar-small {
  height: 22px;
}

.x6-toolbar.x6-toolbar-small .x6-toolbar-content-extras {
  font-size: 12px;
}

 
.x6-toolbar.x6-toolbar-small .x6-toolbar-item-icon {
  font-size: 12px;
}

.x6-toolbar.x6-toolbar-small .x6-toolbar-item.x6-toolbar-item-active {
  margin: 2px 0;
}

.x6-toolbar.x6-toolbar-hover-effect.x6-toolbar-small {
  height: 28px;
}

 
 
.x6-toolbar.x6-toolbar-align-right .x6-toolbar-content {
  flex-direction: row-reverse;
}

.canvasToolbar___3oTpH {
  //background-color: #f7f9fb;
}

.canvasToolbar___3oTpH .x6-toolbar {
  height: 36px !important;
  overflow: visible;
  border-bottom: 1px solid #ececec;
}

.canvasToolbar___3oTpH .x6-toolbar .x6-toolbar-content {
  overflow: visible;
}

.canvasToolbar___3oTpH .x6-toolbar .x6-toolbar-content .x6-toolbar-group::before {
  margin: 0 !important;
}

.x6-toolbar-item {
  margin: 0px 0 ;
  padding: 0 8px ;
  height: 30px;
}

.canvasToolbar___3oTpH .x6-toolbar .x6-toolbar-content .x6-toolbar-item .anticon {
  font-size: 16px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}

.canvasToolbar___3oTpH .searchItem___ykSJx:hover {
  background-color: transparent !important;
}

.canvasToolbar___3oTpH .searchItem___ykSJx .searchWrapper___3xNF3 {
  position: relative;
}

.canvasToolbar___3oTpH .searchItem___ykSJx .searchWrapper___3xNF3 .search___L4h45 {
  width: 200px;
  background-color: transparent;
  animation: expansion___265Ic 0.2s ease-in-out;
}

@keyframes expansion___265Ic {
  from {
    width: 0;
  }

  to {
    width: 200px;
  }
}

.canvasToolbar___3oTpH .searchItem___ykSJx .searchWrapper___3xNF3 .search___L4h45 .ant-input {
  background-color: transparent;
}

.canvasToolbar___3oTpH .searchItem___ykSJx .searchWrapper___3xNF3 .searchResult___MYomj {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 1;
  width: 200px;
  max-height: 200px;
  padding: 4px 0;
  overflow-y: auto;
  color: rgba(0, 0, 0, 0.65);
  list-style-type: none;
  background-color: #fefefe;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}

.canvasToolbar___3oTpH .searchItem___ykSJx .searchWrapper___3xNF3 .searchResult___MYomj.hide___2XGM9 {
  display: none;
}

.canvasToolbar___3oTpH .searchItem___ykSJx .searchWrapper___3xNF3 .searchResult___MYomj .searchResultItem___3whrr {
  padding: 0 8px;
  line-height: 24px;
  text-align: left;
}

.canvasToolbar___3oTpH .searchItem___ykSJx .searchWrapper___3xNF3 .searchResult___MYomj .searchResultItem___3whrr:hover {
  background-color: rgba(243, 249, 255, 0.92);
}

.rt-input {
  font-size: 12px;
  display: block;
  margin: 4px;
  padding-left: 8px;
  color: #333;
  background: #fff;
  border: 1px solid #e9e9e9;
  border-radius: 12px;
  outline: 0;
}

.svg-text {
  font-size: 12px;
  padding-left: 4px;
}
</style>